//
//  LoginViewController.m
//  SinaKanDian
//
//  Created by lina on 11-11-1.
//  Copyright (c) 2011年 Sina. All rights reserved.
//

#import "ModifyInfoViewController.h"

//
//#define Identifier_User @"Identifier_User"
//#define Identifier_Key  @"Identifier_Key"
//#define CellText_Font   16
//#define LoginButton_Font 15
//#define LoginTableView_SectionHeaderHeight 10
//#define LoginTableView_RowHeight 50
//#define LoginTableView_RowWidth 300
//#define LoginTextField_TopPadding 13
//#define LoginTextField_LeftPadding 10


@interface ModifyInfoViewController ()

- (void) initTableView;
- (void) initNavigationBar;
- (void) initSegment;

//- (UITableViewCell*) tableViewCellWithReuseIdentifier:(NSString *)identifier;

@end

@implementation ModifyInfoViewController

@synthesize tipView = _tipView;




#pragma mark - View lifecycle
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    sexIndex = -1;
    [self initNavigationBar];
    [self initTableView];
    [self initSegment];
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg_texture.png"]];
}

- (void)viewDidUnload
{
	_loginTableView.dataSource = nil;
    [_tipView release];_tipView = nil;
    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
//    return (interfaceOrientation == UIInterfaceOrientationPortrait);
    return NO;
}

- (void)dealloc
{
	_loginTableView.dataSource = nil;
    [_loginTableView release];
    [_nameTextField release];
    [_phoneTextField release];
    [_tipView hide];
    [super dealloc];
}

#pragma mark - tableview source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSString *identifier = @"UITableViewCell";
    
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) 
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    CGRect rect = CGRectMake(10, 10, 300, 40);
    UITextField * textField;

    if(indexPath.section == 0)
    {
        _nameTextField = [[UITextField alloc] initWithFrame:rect];
        textField = _nameTextField;
        textField.secureTextEntry = NO;
        textField.placeholder = @"请填写您的真是姓名,方便朋友找到你";
    }
    else {
        _phoneTextField = [[UITextField alloc] initWithFrame:rect];
        textField = _phoneTextField;
        textField.secureTextEntry = YES;
        textField.placeholder = @"你的手机号码";

    }

    textField.font = [UIFont systemFontOfSize:14];
    textField.clearButtonMode = YES;
    textField.backgroundColor = [UIColor clearColor];
    textField.delegate = self;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell.contentView addSubview:textField];
    
    return cell;

}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSString*   string = nil;
    if(section == 0)
    {
        string = @"姓名:";
    }
    else if(section == 1)
    {
        string = @"手机号码:";
    }
    return string;
    
}


#pragma mark - class method


- (void) initTableView
{
    _loginTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0,self.view.bounds.size.width, self.view.bounds.size.height) style:UITableViewStyleGrouped];
    _loginTableView.dataSource = self;
    _loginTableView.rowHeight = 40;
    _loginTableView.scrollEnabled = NO;
   _loginTableView.backgroundColor = [UIColor clearColor];
    _loginTableView.sectionHeaderHeight = 20;
    _loginTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_loginTableView];
}

- (void) initNavigationBar
{
    // title
    UILabel* tempLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 120, 44)];
    [tempLabel setText:@"修改个人信息"];
    [tempLabel setBackgroundColor:[UIColor clearColor]];
    [tempLabel setTextAlignment:UITextAlignmentCenter];
    [tempLabel setTextColor:[UIColor colorWithRed:111/255.0 green:117/255.0 blue:81/255.0 alpha:1.0]];
    [tempLabel setFont:[UIFont boldSystemFontOfSize:20]];
    self.navigationItem.titleView = tempLabel;
    [tempLabel release];
    tempLabel = nil;
    
    
    UIImage  *backImage  = [UIImage imageNamed:@"btn_back_normal.png"];
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(0, 0, 60, 30);
	[leftButton setBackgroundImage:backImage forState:UIControlStateNormal];
    [leftButton setBackgroundImage:[UIImage imageNamed:@"btn_back_press.png"] forState:UIControlStateHighlighted];
    [leftButton addTarget:self action:@selector(cancelButtonPress) forControlEvents:UIControlEventTouchUpInside];
    
    
    UIBarButtonItem*  leftButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    //[leftButton release];     
    //leftButton     = nil;
    self.navigationItem.leftBarButtonItem = leftButtonItem;    
    [leftButtonItem release];
    leftButtonItem = nil;
    
    
    UIImage  *registImage = [UIImage imageNamed:@"btn_finish_normal.png"];
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    rightButton.frame = CGRectMake(0, 0, 50, 30);
    
	[rightButton setBackgroundImage:registImage forState:UIControlStateNormal];
    [rightButton setBackgroundImage:[UIImage imageNamed:@"btn_finish_press.png"] forState:UIControlStateHighlighted];
    [rightButton addTarget:self action:@selector(finishButtonPress) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem*  rightButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    self.navigationItem.rightBarButtonItem = rightButtonItem;    
    [rightButtonItem release];
    rightButtonItem = nil;

}

- (void) initSegment
{
    NSArray *itemArray = [NSArray arrayWithObjects: @"男", @"女",nil];  
    UISegmentedControl *_segmentOfPurposeOfAppointment = [[UISegmentedControl alloc] initWithItems: itemArray];  
    _segmentOfPurposeOfAppointment.frame = CGRectMake(80, 160, 160, 30);  
    _segmentOfPurposeOfAppointment.segmentedControlStyle = UISegmentedControlStylePlain;  
//    _segmentOfPurposeOfAppointment.selectedSegmentIndex = 0;  
    [_segmentOfPurposeOfAppointment addTarget:self  
                                       action:@selector(pickOne:)  
                             forControlEvents:UIControlEventValueChanged];  
    [self.view addSubview:_segmentOfPurposeOfAppointment];
    [_segmentOfPurposeOfAppointment release];
}

#pragma 
#pragma mark  user   button  press
- (void) cancelButtonPress
{
    [_tipView hide];
    [self.navigationController popViewControllerAnimated:YES];


}
- (void) finishButtonPress
{
    if (!_nameTextField.text && [_nameTextField.text isEqualToString:@""]) {
        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入姓名！" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        [alert release];
        return;
    }
    else if ([_phoneTextField.text isEqualToString:@""])
    {
        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入电话！" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        [alert release];
        return;
    }
    else if (sexIndex == -1)
    {
        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请选择性别！" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        [alert release];
        return;
    }
    NSString* sex = [NSString string];
    if (sexIndex) {
        sex = @"male";
    }
    else {
        sex = @"female";
    }
    NSDictionary* info = [[NSDictionary alloc] initWithObjectsAndKeys:_nameTextField.text,@"name",sex,@"sex",nil];
    CoreMsg* msg = [[CoreMsg alloc] init];
    msg.data = UPDATEPERSONINFO;
    msg.ui_delegate = self;
    
    [[NetDataSource shareInstance]postTo:@"http://www.wxlvtu.com/?a=do_update_personal_information" postInfo:info userInfo:msg];
}

#pragma 
#pragma mark -- textfield delegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    
    [_nameTextField resignFirstResponder];
    [_phoneTextField resignFirstResponder];
    return YES;

}

- (void)pickOne: (id)sender  
{  
    UISegmentedControl *segmentControl = (id)sender;  
    NSInteger segment = segmentControl.selectedSegmentIndex;   
    if (segment == 0)   
    {  
        sexIndex = 0;
    }  
    else if (segment == 1)  
    {
        sexIndex = 1;
    }
}

#pragma -mark DataSourceDelegate

- (void)onDataSourceSucCallback:(CoreMsg *)coreMsg withData:(id)data
{
    if ([coreMsg.data isEqual:UPDATEPERSONINFO])
    {
        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"修改成功！" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        [alert release];
    }
}

- (void)onDataSourceFailCallback:(CoreMsg *)coreMsg withData:(id)data
{
    if ([coreMsg.data isEqual:UPDATEPERSONINFO])
    {
        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"修改失败啦！" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        [alert release];
    }
}

@end

